﻿@{
    ViewBag.Title = "TestEventSource";
}

<h2>TestEventSource</h2>

<script type="text/javascript">
    function connect(url) {
        var evtSource = new EventSource(url);
        evtSource.onmessage = function (e) {
            var newElement = document.createElement("div");
            newElement.innerHTML = "message: " + e.data;
            var main = document.querySelector("#main");
            main.appendChild(newElement);
        }

        evtSource.addEventListener("ping", function (e) {
            var newElement = document.createElement("div");
            newElement.innerHTML = "message: " + e.data;
            var main = document.querySelector("#main");
            main.appendChild(newElement);
        }, false);

        evtSource.onerror = function (e) {
        }
        evtSource.onopen = function (e) {
        }
        return evtSource;
    }
    var outerSource = connect("/Home/EventSourceDateSource");
    setInterval(function () {
        if (outerSource.readyState == 2) {
            outerSource = connect("/Home/EventSourceDateSource");
        }
    },20000);
    
</script>

<div id="main">
</div>

